(function($){
		  $.fn.extend({
			  /*op = {
			   * onOutof:function(){}超过限制数量处理
			   * multiple:true 多图上传，默认单图
			   * fileName:"name" 上传文件name
			   * initFileName:"initName"
			   * initData:[]初始化
			   * standard:true;//是否标准模式上传 (file[0],file[1])
			   * clickView:function(){
			   * }
			   * style:按钮样式
			   * readonly:flase;
			   * }*/
			  lxUpload:function(op){
				  op = op||{};
				  op.num = op.num||1;
				  this.append('<div class="lx-view" style="display: inline-block;"></div>');
				  if(!op.readonly)
					  this.append('<div class="lx-input" style="position:relative;display:inline-block;width:80px;height: 92px;background-size: 50px;background-position: center;">'+
							('<input class="'+op.style+'" type="button" value="选择文件" onclick="$(this).next().click()" style="position:absolute;top:0px;left:0" ><input type="file" style="display:none;" title="请选择图片" accept="image/*"  >')+'</div>')
					
					
					this.find('>.lx-input>input[type=file]').change(onSelectFile);
					var context = this;
					//初始化
					if(op.initData&&op.initData.length>0){
						var imgbox = context.find(">.lx-view");
						if(op.multiple)
							for (var i = 0; i < op.initData.length; i++) {
								addView(op.initData[i],imgbox,op.initFileName,op.clickView);
							}
						else addView(op.initData[0],imgbox,op.initFileName,op.clickView);
							
						
						
					}
					
					
					function onSelectFile(e){
						
						
						if(op.multiple){
							
							if(context.find(">.lx-view>div").length>=op.num){
								if(op.onOutof)op.onOutof();
								else alert("文件数过多");
								return;
							}
							
							
						}else context.find(">.lx-view").empty();
						
						addView($(e.target),context.find(">.lx-view"),op.fileName,op.clickView);
						
						var f = $('<input style="display:none;" type="file"  title="请选择图片" accept="image/*">')
						f.change(onSelectFile);
						context.find(">.lx-input").append(f);
						
					}
					
					function onchanage(){
						var fIndex = 0;
						var initIndex = 0;
						context.find(">.lx-view>div>input").each(function(i,e){
							if(op.fileName&&op.fileName == op.initFileName){
								$(e).attr("name",op.fileName+"["+i+"]");
							}else if(op.fileName&&$(e).attr("data-type")=="file"){
								$(e).attr("name",op.fileName+"["+fIndex+"]");
								fIndex++;
							}else if(op.initFileName&&$(e).attr("data-type")=="val"){
								$(e).attr("name",op.initFileName+"["+initIndex+"]");
								initIndex++;
							}
								
							
						});
					}
					
					//图片展示
					function addView(imgFile,imgBox,fileName,clickView) {
						var type= "";
						if(typeof(imgFile)!="string"){
							type="val";
							if(fileName)imgFile.attr("name",fileName);
							
							var imgName = imgFile[0].files[0].name;
							
						}else{
							type="file";
							var imgName = imgFile;
						}
						
						
						var item = $('<div data-type="'+type+'" style="display: inline-block;margin: 0 10px;width:80px;height: 80px;"><img style="width:80px;height: 80px;" title=' + imgName + ' alt=' + imgName + ' src=' +' >'+
								(op.readonly?'':'<p style="left: 0;width: 100%; height: 15px;background: black;text-align: center;line-height: 15px;color: white; font-size: 12px;cursor: pointer;">删除</p>')+'</div>');
						
						if(clickView){
							item.find(">img").click(clickView);
							
						}
						
						setImg(imgFile,item.find(">img"));
						
						item.find("p").click(removeImg);
						if(typeof(imgFile)!="string"){
							item.append(imgFile);
						}else{
							item.append('<input name="'+(fileName?fileName:"")+'" type="hidden" value="'+imgFile+'"/>');
						}
						
						imgBox.append(item);
						if(op.standard)onchanage();
						
					}
					
					//图片预览路径
					function setImg(file,img) {
						if(typeof(file)=="string"){
							$(img).attr("src",file);
							return;
						}
						
						  var ifile = file[0].files[0];
					      var reader = new FileReader();
					      reader.readAsDataURL(ifile);
					      reader.onload = function(e){
					    	  $(img).attr("src",this.result);
					      }
					  }
					
					function imgDisplay(e) {
						var src = $(e).attr("src");
						var imgHtml = $('<div style="display:table;width: 100%;height: 100vh;overflow: auto;background: rgba(0,0,0,0.5);text-align: center;position: fixed;top: 0;left: 0;z-index: 1000;"><div style="display: table-cell;vertical-align: middle;"><img src=' + src + ' /></div><p style="font-size: 50px;position: fixed;top: 30px;right: 30px;color: white;cursor: pointer;" >×</p></div>');
						imgHtml.click(closePicture);
						$('body').append(imgHtml);
					}
					//关闭
					function closePicture(e) {
						$(e.target).parent("div").remove();
					}
					
					function removeImg(e) {
						var box = $(e.target).parent().parent();
						$(e.target).parent().remove();
						if(op.standard)onchanage();
					}
					
				
				
			}
		})
		})(jQuery);